#coding=utf-8
from goods.models import *
from django.db.transaction import atomic  #事务
import json
# import os,django
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myShop.settings")
# django.setup()
# {
# "category": "\u5973\u88c5",
# "goods": [
# {"sizes": [["150", "150"], ["160", "160"], ["165", "165"], ["170", "170"]],
# "goods_oldprice": "499.00",
# "goods_desc": "\u68a6\u5a1c\u4e16\u5bb62017\u5973\u5f0f\u65b0\u6b3e\u4fee\u8eab\u4e2d\u957f\u6b3e\u6bdb\u9886\u65f6\u5c1a\u663e\u7626\u6b27\u7f8e\u6c14\u8d28\u7fbd\u7ed2\u670dA88",
#  "colors": [["\u7ea2\u8272", "/media/color/hong_Dm4fQ6U.jpg"], ["\u7eff\u8272", "/media/color/lv_cTMJg2K.jpg"], ["\u9ec4\u8272", "/media/color/huang_bvCMlhn.jpg"], ["\u9ed1\u8272", "/media/color/hei_rFOWelp.jpg"]],
#  "goodsname": "90\u7ed2\u5927\u6bdb\u9886\u4fdd\u6696\u7fbd\u7ed2\u670d",
#  "goods_price": "99.00",
#  "specs": [["\u53c2\u6570\u89c4\u683c", ["/static/img/%E8%AF%A6%E6%83%85%E9%A1%B5_03.png"]], ["\u6574\u4f53\u6b3e\u5f0f", ["/static/img/%E8%AF%A6%E6%83%85%E9%A1%B5_06.png"]], ["\u6a21\u7279\u5b9e\u62cd", ["/media/1_mpwtoGA.jpg", "/media/2_UuQkY4b.jpg", "/media/3_ViMgWv6.jpg", "/media/4_BDmgdFv.jpg", "/media/5_ozWIsej.jpg", "/media/6_Pny8yTQ.jpg", "/media/7_K4tB09L.jpg", "/media/8_60MJMwS.jpg", "/media/9_8YomGSk.jpg", "/media/10_vonnLjk.jpg"]]]
#  }
#  ]
#  }

@atomic
#读取json数据文件，然后导入数据库
def test_model():
    #读取json文件
    with open('./jiukuaijiu.json') as fr:
        #将文件内容转化为json的数据
        datas = json.loads(fr.read())
        #遍历列表，列表中的data是个字典
        for data in datas:
            #创建类别对象并保存到数据库
            cate = Category.objects.create(name=data['category'])
            #获取类别下的所有商品
            _goods = data['goods']
            #遍历所有的商品
            for goods in _goods:
                #创建商品的基本信息模型对象并保存到数据库
                good = Goods.objects.create(name=goods['goodsname'], desc=goods['goods_desc'],
                                            price=goods['goods_price'], oldprice=goods['goods_oldprice'],
                                            category=cate)
                #商品尺码
                sizes = []
                for _size in goods['sizes']:
                    #数据库查重
                    if Size.objects.filter(name=_size[0]).count() == 1:
                        size = Size.objects.get(name=_size[0])
                    else:
                        size = Size.objects.create(name=_size[0], value=_size[1])
                    sizes.append(size)
                #商品颜色
                colors = []
                for _color in goods['colors']:
                    color = Color.objects.create(name=_color[0], value=_color[1])
                    colors.append(color)
                #商品详情
                for _spec in goods['specs']:
                    goodsdetails = GoodsDetails.objects.create(name=_spec[0])
                    for img in _spec[1]:
                        GoodsDetailsItem.objects.create(goods=good,values=img, goodsdetails=goodsdetails)
                #库存信息
                for c in colors:
                    for s in sizes:
                        Inventory.objects.create(goods=good, color=c, size=s)

#清除所有数据
def deleteall():
    Category.objects.filter().delete()
    Color.objects.filter().delete()
    Size.objects.filter().delete()

if __name__ == '__main__':
    test_model()